package com.aruba.camera3dapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class CameraDemoView extends View {
    private Camera camera = new Camera();
    private Bitmap bitmap;
    private Paint mPaint = new Paint();
    private Matrix matrix = new Matrix();

    public CameraDemoView(Context context) {
        this(context, null);
    }

    public CameraDemoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CameraDemoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.zuobiao, options);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        matrix.reset();
        //涉及到矩阵变换，和canvas同样的，我们使用save和restore对矩阵保存和恢复
        camera.save();
        //往x，y，z方向分别平移100像素
        //camera.translate(100, 100, 100);
        //绕x轴旋转45度
        camera.rotateX(45);
        //每次我们都要获取下矩阵
        camera.getMatrix(matrix);

        camera.restore();

        //矩阵前乘
        matrix.preTranslate(-bitmap.getWidth() / 2f,-bitmap.getHeight() / 2f);
        //矩阵后乘
        matrix.postTranslate(bitmap.getWidth() / 2f,bitmap.getHeight() / 2f);
        canvas.drawBitmap(bitmap, matrix, mPaint);
    }
}
